
import Common from './common';

export default class ButtonAction extends Common {
    constructor(comp) {
        super();
        this.comp = comp;
        this.action = comp.action;
    }

    didAllInstance = () => {
        this.setBtnMap();
    }

    // 第一次进页面的按钮状态
    initPageButton = () => {
        const {props} = this.comp;
        const {
            button
        } = props;

        button.setButtonVisible({
            save: false,
            cancel: false,
            first: false,
            pre: false,
            next: false,
            last: false,
            copy: false,
            aux_func: true,
            approveinfo: false,
            edit: false,
            file: false,
            checknote: false,
            execute: true,
            commit: true,
            check_appr_opini: false,
            sendMsg: true
        });
        button.setButtonDisabled({
            add: true,
            edit: true,
            delete: true,
            copy: true,
            search: true,
            commit: true,
            assfunc: true,
            print: true,
            batchadd: true,
            callback: true,
            approveinfo: true,
            refresh: true
        });
    }

    // 获取主表数据
    getMainTableData = () => {
        this.pubSub.publish('getMainTableData');
    }

    // 设置按钮点击事件的map结构
    setBtnMap = () => {
        this.buttonMapFunc = {
            // 查询
            'search': this.showSearchModal,
            // 刷新
            'refresh': this.getMainTableData,
            // 新增
            'add': this.action.formAct.toAddPage,
            // 删除
            'del': this.action.rowAct.deleteRows,
            // 提交
            'commit': this.action.rowAct.submitOnes,
            // 收回
            'recover': this.action.rowAct.callbackOnes,
            // 查看编制情况
            'CheckBudget': this.action.orgSta.openOrgSituationModal,
            // 批量新增
            'batchAdd': this.action.batAddAct.openBatchModal,
            // 打印
            'print': this.action.mainAct.printTable,
            // 输出
            'out': this.action.mainAct.outputTable,
            // 查看合同
            'viewctrt': this.action.vcAct.showViewContactModal,
            // 执行
            'execute': this.action.rowAct.openExecModal,
            // 发送通知
            'sendMsg': this.sendMessage,
            // 联查人员卡片
            'cardrpt': this.previewCardView
        }
    }

    // 联查人员卡片
    previewCardView = () => {
        let rows = this.comp.props.editTable.getCheckedRows('list');

        this.wpsview.openView(rows);
    }

    // 发送通知
    sendMessage = () => {
        this.pubSub.publish('sendMessage');
    }

    // 按钮点击回调
    headerClick = (props, btnCode) => {
        const {action} = this.comp;
        
        console.log(btnCode);

        typeof this.buttonMapFunc[btnCode] === 'function' && this.buttonMapFunc[btnCode]();
    }

    // 展示查询弹窗
    showSearchModal = () => {
        const {props} = this.comp;
        const {search, emp, meta, dispatch} = props;
        let template = meta.getMeta();

        search.openAdvSearch('search', true);

        let prevCondition = emp.historyTableQueryCondition.querycondition.conditions

        template['search'].items.map((item) => {
            if(item.attrcode === 'pk_psnjob') {
                if(!item.queryCondition) {
                    item.queryCondition = {};
                }
                Object.assign(item.queryCondition, {
                    GridRefActionExt: "nccloud.web.hryf.partapply.sqlbuilder.PartSearchPsnJobRefSqlBuilder",
                    pk_org: emp.orgValue.refpk
                });
            }
        });

        if(prevCondition) {
            search.setSearchValue('search', prevCondition);
            this.update({
                historyTableQueryCondition: {
                    querycondition: {}   
                }
            });
        }
    }

    // 人力组织改变
    changeOrg = (value) => {
        const {props} = this.comp;
        const {
            dispatch,
            button
        } = props;

        if(value) {
            button.setButtonDisabled({
                add: false,
                edit: false,
                delete: false,
                copy: false,
                search: false,
                commit: false,
                aux_func: false,
                print: false,
                batchAdd: false,
                recover: false,
                check_appr_opini: false,
                refresh: false
            });
        }
        dispatch({
            type: 'emp/update',
            payload: {
                orgValue: value
            }
        });
        // this.pubSub.publish('getMainTableData', {
        //     pk_org: value.refpk
        // });
    }
}